dojo.provide("lorelei.mml.markup");

lorelei.mml.markup.meta = {
"@prefix": "<http://purl.org/net/ns/doas#>",
"@about": "<http://mabinogi.logue.be/js/lolerei/mml/markup.js>", a: ":JavaScript",
 title: "Lorelei MML MarkUp script",
 created: "2008-07-29", release: {revision: "1.6.3", created: "2008-10-13"},
 author: {name: "Logue", homepage: "<http://logue.be/>"},
 license: "<http://www.gnu.org/licenses/gpl-3.0.html>"
};

// Lorelei MML MarkUp script.
// Copyright (c)2008 Logue <http://logue.be/> All rights reserved.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

console.log(lorelei.mml.markup.meta.title,lorelei.mml.markup.meta.release,' has been loaded!');

dojo.addOnLoad(function (){
	
	dojo.query('pre.mml').forEach(function(elem) {
		var mml;
		if (elem.textContent){
			mml = elem.textContent;
		}else{
			mml = elem.innerText;	// IE
		}

		elem.innerHTML = lorelei.mml.markup(mml);
	});
});

lorelei.mml.markup = function (str,option){
	str = str.replace(/\r\n/g, "");//IE
	str = str.replace(/\n/g, "");//Firefox
	
	str.match(/(MML@)\s*([\s0-9a-glnortvA-GLNORTV#<>.&+-]*),\s*([\s0-9a-glnortvA-GLNORTV#<>.&+-]*),\s*([\s0-9a-glnortvA-GLNORTV#<>.&+-]*);/g);

	if (RegExp.$1 == 'MML@'){
		var MMLs = [RegExp.$2, RegExp.$3, RegExp.$4];
		var track = [];
		for(var partid = 0; partid < MMLs.length; partid++) {
			var output = [];
			var mml_notes = MMLs[partid].match(/[abcdefglnortvABCDEFGLNORTV<>][\+\#-]?[0-9]*\.?&?/g);
			if (mml_notes == null){
				track[partid] = '';
				continue;
			}
			var bytes = 0;
			for(var mnid=0; mnid < mml_notes.length; mnid++) {
				var mml_note = mml_notes[mnid];
				if( mml_note.match(/([lortvLORTV<>])([1-9][0-9]*|0?)(\.?)(&?)/)) {
					switch(RegExp.$1){
						case 'T': case 't':
							output[mnid] = '<span class="tempo">' + RegExp.$1 + RegExp.$2 + '</span>';
						break;
						case 'L': case 'l':
							output[mnid] = '<span class="length">' + RegExp.$1 + RegExp.$2 + '</span>';
							if(RegExp.$3 == '.') output[mnid] += '<span class="dotted">.</span>';
							if(RegExp.$4 == '.') output[mnid] += '<span class="tie">&amp;</span>';
						break;
						case 'O': case 'o':
							output[mnid] = '<span class="octave">' + RegExp.$1 + RegExp.$2 + '</span>';
						break;
						case 'R': case 'r':
							output[mnid] = '<span class="rest">' + RegExp.$1 + RegExp.$2 +  '</span>';
						break;
						case 'V': case 'v':
							output[mnid] = '<span class="volume">' + RegExp.$1 + RegExp.$2 +'</span>';
						break;
						case '<': 
							output[mnid] = '<span class="octave2">&lt;</span>';
						break;
						case '>':
							output[mnid] = '<span class="octave2">&gt;</span>';
						break;
					}

				} else if(mml_note.match(/([nN])(0|[1-8][0-9]?|9[0-6]?)(&?)/)) {
					output[mnid] = '<span class="note">' + RegExp.$1 + RegExp.$2 + '</span>';
					if(RegExp.$3 == '&') output[mnid] += '<span class="tie">&amp;</span>';

				} else if(mml_note.match(/([abcdefgABCDEFG])([\+\#-]?)([1-9][0-9]*|0?)(\.?)(&?)/)) {
					if (RegExp.$1){ 
						output[mnid] = RegExp.$1;
					}else{
						output[mnid] = '';
					}

					if (RegExp.$2 == '+' || RegExp.$2 == '#' || RegExp.$2 == '-') 
						output[mnid] += '<span class="chromatic">' + RegExp.$2 + '</span>';
					if (RegExp.$3 != '') 
						output[mnid] += '<span class="length2">' + RegExp.$3 + '</span>';
					if (RegExp.$4 == '.') 
						output[mnid] += '<span class="dotted" connectId="dotted">' + RegExp.$4 + '</span>';
					if (RegExp.$5 == '&') 
						output[mnid] += '<span class="tie" connectId="tie">&amp;</span>';
				}else{
					output[mnid] = '<span class="unknown">' + mml_note + '</span>';
				}
				bytes = mml_note.length + bytes;
				if (76 <= bytes && dojo.isMozilla){	// FireFox cannot auto return.
					output[mnid] += '<br />';
					bytes = 0;
				}
			}
//			console.log(mml_notes);
			track[partid] = output.join('') + '<br />';
		}
		var output = '<span class="ext">MML@</span><br />' + track[0] + ',<br />' + track[1] + ',<br />' + track[2] + ';';
		return output;
	}else{
		alert(message['unknown_format']);
	}
}